local skel = fk.CreateSkill {
  name = "lb_zero__caichen",
}

Fk:loadTranslationTable {
  ["lb_zero__caichen"] = "裁忱",
  --[":lb_zero__caichen"] = "当你响应或使用一张实体牌后，若此牌牌名不在<a href=':lb_zero__tingyi'><font color='red'>〖霆毅〗</font></a>的连招条件中，你可将之牌名替换其中的一个连招条件并获得之，否则你可以视为使用你的响应牌，若响应牌由〖霆毅〗转化，改为响应牌的实体牌。",
  --11.19
  [":lb_zero__caichen"] = "当你响应一张实体牌或你因弃置仅失去一张牌后，若此牌牌名不在<a href=':lb_zero__tingyi'><font color='red'>〖霆毅〗</font></a>的连招条件中，你可将之牌名替换其中的一个连招条件并获得之，否则你可以视为使用你的响应牌。",

  ["#lb_zero__caichen"] = "裁忱：你可以将 %arg 替换〖霆毅〗的一个连招条件并获得 %arg2",
  ["#lb_zero__caichen-change"] = "裁忱：你可以将 %arg 替换〖霆毅〗的一个连招条件",
  ["#lb_zero__caichen-use"] = "裁忱：你可以视为使用 %arg",

  ["$lb_zero__caichen1"] = "抗拒从严！",
  ["$lb_zero__caichen2"] = "那我就不客气啦！",
  ["$lb_zero__caichen3"] = "全部缴械！",
}

local caichen_spec = {
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) and data.responseToEvent then
      local card = data.responseToEvent.card
      local cards = Card:getIdList(card)
      if #cards == 1 then
        local real_card = Fk:getCardById(cards[1])
        local combo_list = player:getTableMark("@[lb_zero__tingyi]lb_zero__tingyi").combo_list or {}
        if table.contains(combo_list, real_card.trueName) then
          local name = data.card.trueName
          if table.contains(data.card.skillNames, "lb_zero__tingyi") and data.card:getEffectiveId() then
            name = Fk:getCardById(data.card:getEffectiveId()).trueName
          end
          event:setCostData(self, { name = name })
          return true
        else
          event:setCostData(self, { cards = cards })
          return player:hasSkill("lb_zero__tingyi", true)
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local name = event:getCostData(self).name
    local room = player.room
    if name then
      local use = room:askToUseVirtualCard(player, {
        name = name,
        skill_name = skel.name,
        skip = true,
        prompt = "#lb_zero__caichen-use:::" .. name,
      })
      if use then
        event:setCostData(self, { use = use })
        return true
      end
    else
      local choices = table.simpleClone(player:getTableMark("@[lb_zero__tingyi]lb_zero__tingyi").combo_list or {})
      if #choices > 0 then
        table.insert(choices, "Cancel")
        local card = Fk:getCardById(event:getCostData(self).cards[1])
        local prompt = "#lb_zero__caichen:::" .. card.trueName .. ":" .. card:toLogString()
        local cards = table.filter(event:getCostData(self).cards, function(id)
          return event == fk.AfterCardsMove
          and player.room:getCardArea(card) == Card.DiscardPile
          or player.room:getCardArea(id) == Card.Processing
        end)
        if #cards == 0 then
          prompt = "#lb_zero__caichen-change:::" .. card.trueName
        end
        local choice = room:askToChoice(player, {
          choices = choices,
          skill_name = skel.name,
          cancelable = true,
          prompt = prompt
        })
        if choice ~= "Cancel" then
          event:setCostData(self, { card_name = card.trueName, cards = cards, choice = choice })
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local use = event:getCostData(self).use
    if use then
      room:useCard(use)
    else
      local cards = event:getCostData(self).cards
      local list = player:getTableMark("@[lb_zero__tingyi]lb_zero__tingyi")
      local combo_list = list.combo_list or {}
      for i, val in ipairs(combo_list) do
        if val == event:getCostData(self).choice then
          combo_list[i] = event:getCostData(self).card_name
          break
        end
      end
      list.combo_list = combo_list
      room:setPlayerMark(player, "@[lb_zero__tingyi]lb_zero__tingyi", list)
      if #cards > 0 then
        room:obtainCard(player, cards, true, fk.ReasonJustMove, player, skel.name)
      end
    end
  end,
}
--[[
skel:addEffect(fk.CardUseFinished, {
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      local use_cards = Card:getIdList(data.card)
      local combo_list = player:getTableMark("@[lb_zero__tingyi]lb_zero__tingyi").combo_list or {}
      if #use_cards == 1 then                                      -- 使用牌后
        local real_card = Fk:getCardById(use_cards[1])
        if not table.contains(combo_list, real_card.trueName) then -- 不在连招条件中
          return player:hasSkill("lb_zero__tingyi", true)
        end
      end
      if data.responseToEvent and data.responseToEvent.card then
        use_cards = Card:getIdList(data.responseToEvent.card)
        if #use_cards == 1 then                                                                             -- 响应牌后
          local real_card = Fk:getCardById(use_cards[1])
          return table.contains(combo_list, real_card.trueName) or player:hasSkill("lb_zero__tingyi", true) -- 没技能不给换
        end
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      local use_cards = Card:getIdList(data.card)
      if #use_cards == 1 then -- 使用牌后
        local combo_list = player:getTableMark("@[lb_zero__tingyi]lb_zero__tingyi").combo_list or {}
        local real_card = Fk:getCardById(use_cards[1])
        if not table.contains(combo_list, real_card.trueName) then -- 不在连招条件中
          event:setCostData(self, { cards = use_cards })
          self:doCost(event, target, player, data)
        end
      end
      if data.responseToEvent and data.responseToEvent.card then
        local cards = Card:getIdList(data.responseToEvent.card)
        if #cards == 1 then -- 响应牌后
          local combo_list = player:getTableMark("@[lb_zero__tingyi]lb_zero__tingyi").combo_list or {}
          local real_card = Fk:getCardById(cards[1])
          if not table.contains(combo_list, real_card.trueName) then -- 不在连招条件中
            event:setCostData(self, { cards = cards })
          else
            local name = data.card.trueName
            if table.contains(data.card.skillNames, "lb_zero__tingyi") and data.card:getEffectiveId() then
              name = Fk:getCardById(data.card:getEffectiveId()).trueName
            end
            event:setCostData(self, { name = name })
          end
          self:doCost(event, target, player, data)
        end
      end
    end
  end,
  on_cost = caichen_spec.on_cost,
  on_use = caichen_spec.on_use,
})

skel:addEffect(fk.CardRespondFinished, {
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) and data.responseToEvent then
      local card = data.responseToEvent.card
      local cards = Card:getIdList(card)
      if #cards == 1 then
        local real_card = Fk:getCardById(cards[1])
        local combo_list = player:getTableMark("@[lb_zero__tingyi]lb_zero__tingyi").combo_list or {}
        if table.contains(combo_list, real_card.trueName) then
          local name = data.card.trueName
          if table.contains(data.card.skillNames, "lb_zero__tingyi") and data.card:getEffectiveId() then
            name = Fk:getCardById(data.card:getEffectiveId()).trueName
          end
          event:setCostData(self, { name = name })
          return true
        else
          event:setCostData(self, { cards = cards })
          return player:hasSkill("lb_zero__tingyi", true)
        end
      end
    end
  end,
  on_cost = caichen_spec.on_cost,
  on_use = caichen_spec.on_use,
})
--]]

skel:addEffect(fk.CardUseFinished, caichen_spec)
skel:addEffect(fk.CardRespondFinished, caichen_spec)

skel:addEffect(fk.AfterCardsMove, {
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and player:hasSkill("lb_zero__tingyi", true) then
      for _, move in ipairs(data) do
        if move.from == player --and move.proposer == player 
        and move.moveReason == fk.ReasonDiscard and #move.moveInfo == 1 then
          -- 增加等于1的限制，不限制操作者是谁
          local combo_list = player:getTableMark("@[lb_zero__tingyi]lb_zero__tingyi").combo_list or {}
          for _, info in ipairs(move.moveInfo) do
            local card = Fk:getCardById(info.cardId)
            if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) and not table.contains(combo_list, card.trueName) then
              event:setCostData(self, { cards = {info.cardId} })
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = caichen_spec.on_cost,
  on_use = caichen_spec.on_use,
})

return skel
